package com.aabte.mybatis.typehandler;

import com.aabte.mybatis.bean.EmployeeStatus;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class EnumEmployeeStatusTypeHandler implements TypeHandler<EmployeeStatus> {


    @Override
    public void setParameter(PreparedStatement ps, int i, EmployeeStatus parameter, JdbcType jdbcType) throws SQLException {
        Integer code = parameter.getCode();
        String msg = parameter.getMsg();
        ps.setString(i, code.toString());
    }

    @Override
    public EmployeeStatus getResult(ResultSet rs, String columnName) throws SQLException {
        int code = rs.getInt(columnName);
        return EmployeeStatus.getEmployeeStatus(code);
    }

    @Override
    public EmployeeStatus getResult(ResultSet rs, int columnIndex) throws SQLException {
        int code = rs.getInt(columnIndex);
        return EmployeeStatus.getEmployeeStatus(code);
    }

    @Override
    public EmployeeStatus getResult(CallableStatement cs, int columnIndex) throws SQLException {
        int code = cs.getInt(columnIndex);
        return EmployeeStatus.getEmployeeStatus(code);
    }
}
